//
//  SEGMyRootViewController.m
//  SEGUhomeStandard
//
//  Created by 刘山 on 2018/4/12.
//  Copyright © 2018年 刘山. All rights reserved.
//

#import "SEGMyRootViewController.h"
#import "SEGProfileView.h"
#import "SEGSliderChooseView.h"
#import "SEGMenuSingleView.h"
#import "SEGPointMallMenuSingleView.h"

@interface SEGMyRootViewController ()<protocolProfileViewDelegate,protocolSliderChooseViewDelegate,UIScrollViewDelegate,ProtocolMenuSingleViewDelegate>
@property (nonatomic, strong)SEGProfileView *varProfileView;
@property (nonatomic, strong)SEGSliderChooseView *varSliderChooseView;
/**
 4.7.0,积分商城
 */
@property (nonatomic, strong)SEGPointMallMenuSingleView *singleView;
/**
 4.7.0,切换小区
 */
@property (nonatomic, strong)SEGMenuSingleView *changeCommunityView;
@end

@implementation SEGMyRootViewController
#pragma mark - life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNotification];
    
    self.view.backgroundColor = kColor_W;
    self.tableView.backgroundColor = [UIColor clearColor];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self requesUserProfile];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    //判断是否显示活动抽奖气泡
    [self seg_checkActivityLotteryBubble];
}

- (void)viewWillDisappear:(BOOL)animated {
    [[SEGUhomeMediator sharedInstance] seg_hiddenActivityLotteryBubble:nil];
    [super viewWillDisappear:animated];
}

#pragma mark - init
- (void)initNotification {
    //收到活动抽奖推送
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(seg_checkActivityLotteryBubble) name:SEGActivityLotteryNotification object:nil];
}

#pragma mark - send request
/// 判断是否显示活动抽奖气泡
- (void)seg_checkActivityLotteryBubble {
    [[SEGUhomeMediator sharedInstance] seg_checkActivityLotteryBubble:@{@"vc" : self}];
}

#pragma mark - 积分数据请求
//获取 零钱、积分、优惠券 总数
- (void)loadData_Integral {
    [[SEGBanalcesManager sharedManager] requestBanalcesWithParameters:nil finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if(isSuccessed){
            
            __block CGFloat cashTotal = 0.0;
            __block CGFloat balanceTotal = 0.0;
            __block CGFloat integralTotal = 0.0;
            
            NSArray* data = [result valueForKey:@"data"];
            if (![data isKindOfClass:[NSArray class]]) {
                return;
            }
            NSMutableArray<SEGBanalcesModel*>* arrM = [NSMutableArray arrayWithCapacity:data.count];
            
            for (NSDictionary* dic in data) {
                SEGBanalcesModel *tmp = [SEGBanalcesModel yy_modelWithDictionary:dic];
                [arrM addObject:tmp];
            }
            
            [arrM enumerateObjectsUsingBlock:^(SEGBanalcesModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([obj.balancetype integerValue] == SEGBanalceType_Cash) {
                    cashTotal += [obj.balance floatValue]/100;
                    
                }
                if ([obj.balancetype integerValue] == SEGBanalceType_Banalce_Cash ||[obj.balancetype integerValue] == SEGBanalceType_Banalce_Time) {
                    balanceTotal += 1;
                }
                if ([obj.balancetype integerValue] == SEGBanalceType_Integral) {
                    integralTotal += [obj.balance floatValue];
                }
            }];
            //刷新积分
            [self.menuDataSource enumerateObjectsUsingBlock:^(NSArray<SEGMenuListModel *> * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [obj enumerateObjectsUsingBlock:^(SEGMenuListModel * _Nonnull model, NSUInteger idxModel, BOOL * _Nonnull stopModel) {
                    if (model.widgetType.integerValue == SEGEnumShowTypeMineIntegral_HLB) {
                        model.mineIntegral = [NSString stringWithFormat:@"%.0f",integralTotal];
                        [self.tableView reloadRow:idxModel inSection:idx withRowAnimation:UITableViewRowAnimationNone];
                    }
                }];
            }];
            
        }
    }];
}


#pragma mark - 获取登录用户资料
- (void)requesUserProfile {
    //更新头像昵称数据等
    [[SEGProfileManager sharedManager] requestProfilecompleted:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        [SVProgressHUD dismiss];
        if (isSuccessed) {
            [self.tableView reloadData];
        }
    }];
}
#pragma mark - custom delegate

#pragma mark - scrollViewDelegate  只允许下拉加载
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
}
@end

